package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.common.domain.response.RestResponse;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.entity.TeachplanWork;
import com.xuecheng.content.mapper.TeachplanWorkMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanWorkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class TeachplanWorkServiceImpl extends ServiceImpl<TeachplanWorkMapper, TeachplanWork> implements TeachplanWorkService {

    @Autowired
    private CourseBaseService courseBaseService;

    /**
     * <p>
     * 根据作业id查询课程绑定作业记录
     * </p>
     *
     * @param workId 作业id
     * @return RestResponse
     */
    @Override
    public RestResponse getCountByWorkId(Long workId) {

        LambdaQueryWrapper<TeachplanWork> queryWrapper = Wrappers.lambdaQuery(TeachplanWork.class)
                .eq(TeachplanWork::getWorkId, workId);

        int count = this.count(queryWrapper);

        return RestResponse.success(count);
    }

    /**
     * <p>
     * 根据作业id查询绑定的课程计划 返回计划名
     * </p>
     *
     * @param workId 作业id
     * @return RestResponse<List < String>>
     */
    @Override
    public RestResponse<Set<String>> getWorkAssociationPlanName(Long workId) {
        LambdaQueryWrapper<TeachplanWork> queryWrapper = Wrappers.lambdaQuery(TeachplanWork.class)
                .eq(TeachplanWork::getWorkId, workId);

        List<TeachplanWork> teachplanWorks = this.getBaseMapper().selectList(queryWrapper);
        Set<String> nameList = new HashSet<>();
        //绑定课程存在才放入数据
        if (!CollectionUtils.isEmpty(teachplanWorks)) {
            for (TeachplanWork teachplanWork : teachplanWorks) {
                Long courseId = teachplanWork.getCourseId();
                CourseBase courseBase = courseBaseService.getById(courseId);
                nameList.add(courseBase.getName());
            }
        }
        return RestResponse.success(nameList);
    }
}
